package com.lynx.system.controller;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.lynx.system.beans.Admin;
import com.lynx.system.beans.User;
import com.lynx.system.feign.AccountFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountFeign accountFeign;
    @GetMapping("redirect/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }
    @PostMapping("/login")
    public Object login(String username, String password, String type, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        Object object = accountFeign.login(username, password, type);
        String result = null;
        if (object==null){
            modelAndView.addObject("message","用户名或密码错误");
            result = "login";
        }else if("admin".equals(type)){
            Admin  admin = JSON.parseObject(JSON.toJSONString(object),Admin.class);
            session.setAttribute("admin",admin);
            result="main";
        }else if ("user".equals(type)){
            User  user = JSON.parseObject(JSON.toJSONString(object),User.class);
            session.setAttribute("user",user);
            result = "redirect:/menu/redirect/index";
        }
        return result;
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:redirect/login";
    }
}
